/*

0.选择集团维护 or 子公司维护

1.(主表选择省份)子公司新建/更新价格体系 导入 PriceAdjustmentSub
2.集团新建 /更新价格体系 导入 PriceAdjustmentHead
3.海外 PriceAdjustmentOversea


4.审批人员查看填报

*/
public without sharing class PriceAdjustController{


    public String backId {get;private set;}

    public String msg {get; private set;}

    public Boolean isErr {get; private set;}

    public Boolean isSuc {get; private set;}

    public String dataJson {get; private set;}
    public String searchJson  {get;  set;}

    public String saveJson {get;  set;}

    public static String SUB_RECORDTYPE = 'PriceAdjustmentSub';
    public static String GROUP_RECORDTYPE = 'PriceAdjustmentHead';
    public static String OVERSEA_RECORDTYPE = 'PriceAdjustmentOversea';

    public static String ALL_RECORDTYPE = 'AdjustAll';

    public PriceAdjustment__c pad {get;private set;}

    public PriceAdjustController(){
      
        
    }
    public void init(){

        try {
            backId = System.currentPageReference().getParameters().get('id');
            if(String.isBlank(backId))throw new DmlException('页面打开错误');

            pad = [select Id ,Company__c,CompanyDup__c,Province__c,RecordType.Name,RecordType.DeveloperName , Status__c from PriceAdjustment__c where Id =:backId];

            if(pad.RecordType.DeveloperName == ALL_RECORDTYPE ){
                pad.Company__c = '迈克生物股份有限公司';
                pad.Province__c = '四川省/西藏/重庆市';
            }
           if(pad.RecordType.DeveloperName == ALL_RECORDTYPE   && (String.isBlank(pad.CompanyDup__c) || String.isBlank(pad.Company__c) || String.isBlank(pad.Province__c) ) ) throw new DmlException('集团统一调价价格体系公司不能为空'); 

            if(pad.RecordType.DeveloperName == SUB_RECORDTYPE && ( String.isBlank(pad.Company__c) || String.isBlank(pad.Province__c) ))throw new DmlException('价格体系公司或省份不能为空'); 
            if(pad.RecordType.DeveloperName == OVERSEA_RECORDTYPE &&  String.isBlank(pad.Province__c) )throw new DmlException('价格体系省份不能为空'); 
            Map<String,Object>dataMap = new Map<String,Object>();
            dataMap.put('detail', getDetails());
            dataMap.put('searchObject',getSearchObject());
            dataMap.put('search',getNoPriceProduct());
            dataMap.put('combobox',getCombox('PriceAdjustmentDetail__c',new Set<String>{'AdjustmentType__c'}));
            dataJson = EncodingUtil.urlEncode(JSON.serialize(dataMap), 'UTF-8');

        }catch (Exception e) {
            isErr = true;
            ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR ,e.getMessage()+e.getStackTraceString()));
        }
        

    }

     private Map<String ,Map<String,String>> getCombox(String objName ,Set<String> fields ){
        Map<String ,Map<String,String>> tmp = new Map<String ,Map<String,String>>();
        for(String f : fields){
            Map<String,String> pickMap = ImportObject.getPicklistValues(objName, f, true);
            tmp.put(f, pickMap);
        }
        return tmp;
    } 
    public void setNil(){
        dataJson = '[]';
        searchJson = '{}';
        saveJson = '[]';
    }

    //从获取价格体系的调整明细
    public List<ProductBean> getDetails(){
        //ProvincialAgencyPrice__c,NewProvincialAgencyPrice__c,
        Map<Id,Price__c> priceMap  = new  Map<Id,Price__c>();

        List<ProductBean> tmp = new List<ProductBean>();
        List<PriceAdjustmentDetail__c> pdList =  [select Id ,Product__c,Product__r.ProductName__c , Product__r.NCCode__c,Product__r.Category1__c,Product__r.Spec__c,
        Product__r.Unit__c,Product__r.SerialNo__c,Product__r.Model__c,Product__r.Brand__c ,Product__r.ProductStructure__c,Product__r.ProductStructure__r.Name ,
        //子公司
        AreaDealerPrice__c, InvoiceDealerPrice__c,MarketPrice__c,HospitalManagerPrice__c,
        NewAreaDealerPrice__c,NewInvoiceDealerPrice__c,NewMarketPrice__c,NewHospitalManagerPrice__c,
        //海外
        MarketPriceUSD__c,SalesDirectorPrice__c,SalesManagerPrice__c,
        NewMarketPriceUSD__c,NewSalesDirectorPrice__c,NewSalesManagerPrice__c,
        NewHospitalBottomPrice__c,HospitalBottomPrice__c,
        //集团
        SpecialPrice__c,NewSpecialPrice__c,

        Product__r.GroupRegionalDistributorsPrice__c,Product__r.GroupDeclarationDealerPrice__c,Product__r.RepairCost__c,Product__r.swjhdj__c,
        Product__r.GroupMarketPrice__c,Product__r.GroupSpecialPrice__c,Product__r.GroupTerminalGuidePric__c,

        //共用字段
        AdjustmentType__c

         from  PriceAdjustmentDetail__c where  PriceAdjustment__c =:pad.Id  ];
        for(PriceAdjustmentDetail__c p : pdList  ){

            priceMap.put(p.Product__c , null);

        }
         priceMap = ImportObject.getPriceMap(priceMap.keySet(),pad.Company__c ,pad.Province__c ); 

        for(PriceAdjustmentDetail__c p : pdList  ){

            tmp.add(new ProductBean(p,priceMap.get(p.Product__c),pad.RecordType.DeveloperName));

        }


        return tmp;

    }

     private Map<String,List<DataUtil.FieldBean>> getSearchObject(){
        Map<String,List<DataUtil.FieldBean>> searchMap = new Map<String,List<DataUtil.FieldBean>>();

        Map<String,DataUtil.FieldBean>proMap = DataUtil.getFieldsInfo(Product__c.SObjecttype, new Set<String>{'ProductName__c','Brand__c','Spec__c','Model__c','NCCode__c','SerialNo__c','Category1__c','ApplicableInstrumentModel__c'} );
        searchMap.put('Product__c', proMap.values());
        return searchMap;
    }
    //获取有集团价(自产) 没有 子公司价格的产品
    private  List<ProductBean> getNoPriceProduct(){
        //有集团价(自产) 没有 子公司价格
        List<Product__c > pList = [select Id,ProductName__c ,Brand__c ,Spec__c ,SerialNo__c,Unit__c,Category1__c, NCCode__c , Model__c ,ProductStructure__c,ProductStructure__r.Name ,
        GroupRegionalDistributorsPrice__c ,GroupDeclarationDealerPrice__c,RepairCost__c,GroupMarketPrice__c,GroupSpecialPrice__c,GroupTerminalGuidePric__c 
         from Product__c where RecordType.DeveloperName = 'NCProduct'  and GroupPriceSealed__c = false
        and   ( GroupRegionalDistributorsPrice__c != null or GroupDeclarationDealerPrice__c != null 
               or GroupMarketPrice__c != null or GroupTerminalGuidePric__c != null )

         and (not NCCode__c like '13%')   and   (not NCCode__c like '14%')  
        and Id not in 

            (
                select Product__c  from Price__c where Company__c=:pad.Company__c and Province__c =:pad.Province__c 
                and Product__c != null and ClosePrice__c != true 
                and  ( AreaDealerPrice__c != null or InvoiceDealerPrice__c != null or MarketPrice__c != null or  HospitalManagerPrice__c != null   )    
              
            ) limit 1000 ];

        List<ProductBean> tmp = product2Bean(pList ,PriceAdjustController.SUB_RECORDTYPE , pad.Company__c, pad.Province__c );
        return tmp;

    }

    public void search(){
        Map<String,Object> searchMap = (Map<String,Object>) JSON.deserializeUntyped(EncodingUtil.urlDecode(searchJson, 'UTF-8'));

        String sql = 'select Id,ProductName__c ,Brand__c ,Spec__c ,SerialNo__c,Unit__c,Category1__c, NCCode__c , Model__c,ProductStructure__c,ProductStructure__r.Name  ,'+
        'GroupRegionalDistributorsPrice__c ,GroupDeclarationDealerPrice__c,RepairCost__c,GroupMarketPrice__c,GroupSpecialPrice__c,GroupTerminalGuidePric__c '+
        ' from Product__c where RecordType.DeveloperName = \'NCProduct\' ';
        if(String.isNotBlank((String)searchMap.get('ProductName__c'))) sql += ' and ProductName__c like \'%'+searchMap.get('ProductName__c')+'%\' ';
        if(String.isNotBlank((String)searchMap.get('Category1__c'))) sql += ' and Category1__c like \'%'+searchMap.get('Category1__c')+'%\' ';
        if(String.isNotBlank((String)searchMap.get('NCCode__c'))) sql += ' and NCCode__c like \'%'+searchMap.get('NCCode__c')+'%\' ';
        if(String.isNotBlank((String)searchMap.get('Spec__c'))) sql += ' and Spec__c like \'%'+searchMap.get('Spec__c')+'%\' ';
        if(String.isNotBlank((String)searchMap.get('SerialNo__c'))) sql += ' and SerialNo__c like \'%'+searchMap.get('SerialNo__c')+'%\' ';
        if(String.isNotBlank((String)searchMap.get('Brand__c'))) sql += ' and Brand__c like \'%'+searchMap.get('Brand__c')+'%\' ';
        if(String.isNotBlank((String)searchMap.get('Model__c'))) sql += ' and Model__c like \'%'+searchMap.get('Model__c')+'%\' ';
        if( String.isNotBlank((String)searchMap.get('ApplicableInstrumentModel__c'))) sql += '  and ApplicableInstrumentModel__c includes (\''+searchMap.get('ApplicableInstrumentModel__c')+'\') ';

        // if(pad.RecordType.DeveloperName == SUB_RECORDTYPE) sql += ' and (not NCCode__c like \'13%\')   and   (not NCCode__c like \'14%\') and ((brand__c = \'maccura\' and Category0__c not  in (\'配件\' , \'耗材\') )  or brand__c != \'maccura\') ';
         //if(pad.RecordType.DeveloperName == SUB_RECORDTYPE) sql += '  and Category0__c not  in (\'配件\' , \'耗材\') ';
        // if(pad.RecordType.DeveloperName == SUB_RECORDTYPE) sql += '  and ((brand__c = \'maccura\' and Category0__c not  in (\'配件\' , \'耗材\') )  or brand__c != \'maccura\') ';
         if(pad.RecordType.DeveloperName == SUB_RECORDTYPE) sql += ' and (not NCCode__c like \'13%\')   and   (not NCCode__c like \'14%\')  ';

        sql += ' limit 500 ';




        List<Product__c > pList = Database.query(sql);

        List<ProductBean> tmp = product2Bean(pList,pad.RecordType.DeveloperName,pad.Company__c,pad.Province__c);
        dataJson = EncodingUtil.urlEncode(JSON.serialize(tmp), 'UTF-8');


    }
    //将prodouct__c 转换为 bean
    private List<ProductBean>  product2Bean (List<Product__c > pList , String rtType , String company, String province  ){
        Map<Id,Price__c> priceMap  = new  Map<Id,Price__c>();
        //如果不是集团就带出子公司价格
        if(rtType != PriceAdjustController.GROUP_RECORDTYPE){
            for (Product__c p : pList) {
    
                priceMap.put(p.Id, null);
            }
            priceMap = ImportObject.getPriceMap(priceMap.keySet(),company ,province ); 
        }
        List<ProductBean> tmp = new List<ProductBean>();
        for (Product__c p : pList) {
            tmp.add(new ProductBean(p,priceMap.get(p.Id),rtType));
        }
        return tmp;
    }
    // private Map<Id,Price__c> getPriceMap(Set<Id> pids,String c ,String province  ){
    //     Map<Id,Price__c> priceMap  = new  Map<Id,Price__c>();

    //     for(Price__c p :[select Id,Product__c,MarketPrice__c,AreaDealerPrice__c,InvoiceDealerPrice__c,HospitalManagerPrice__c,MarketPriceUSD__c,
    //         SalesmanPrice__c,SalesManagerPrice__c,SalesDirectorPrice__c 
    //         from Price__c where Company__c =:c and Province__c=:province and Product__c in: pids  ]){

    //         priceMap.put(p.Product__c , p);
    //     }

    //     return priceMap;
    // }
    //导入
    @RemoteAction
    public static List<ProductBean> importData(List<String> nccode,String rtType ,String company,String province ){

        List<Product__c> pList = rtType == PriceAdjustController.SUB_RECORDTYPE ?  [select Id,ProductName__c ,Brand__c ,Spec__c ,SerialNo__c,Unit__c,Category1__c, NCCode__c , Model__c ,ProductStructure__c,ProductStructure__r.Name ,
        GroupRegionalDistributorsPrice__c ,GroupDeclarationDealerPrice__c,RepairCost__c,GroupMarketPrice__c,GroupSpecialPrice__c,GroupTerminalGuidePric__c 
         from Product__c where RecordType.DeveloperName = 'NCProduct' and NCCode__c in:nccode  and (not NCCode__c like '13%')   and   (not NCCode__c like '14%') ] : [select Id,ProductName__c ,Brand__c ,Spec__c ,SerialNo__c,Unit__c,Category1__c, NCCode__c , Model__c ,ProductStructure__c,ProductStructure__r.Name ,
        GroupRegionalDistributorsPrice__c ,GroupDeclarationDealerPrice__c,RepairCost__c,GroupMarketPrice__c,GroupSpecialPrice__c,GroupTerminalGuidePric__c 
         from Product__c where RecordType.DeveloperName = 'NCProduct' and NCCode__c in:nccode  ];

        PriceAdjustController pac = new PriceAdjustController();

        return pac.product2Bean(pList,rtType,company,province);


    }
    //保存
    public void save(){
        msg = '';
        try{

            List<Object> saveList =  (List<Object>)JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));
            if(saveList.isEmpty())throw new DmlException('没有数据!');

            List<PriceAdjustmentDetail__c> paList = new List<PriceAdjustmentDetail__c>();
            
            Set<Id> pids = new Set<Id> ();
            Set<Id> details = new Set<Id>();
            Set<Id> seals = new Set<Id> ();

            List<Product__c> pL = new List<Product__c>();
            for (Object o  : saveList) {
                Map<String,Object> mo = (Map<String,Object>) o ;
                PriceAdjustmentDetail__c pdd = new PriceAdjustmentDetail__c();
                pdd.Id = DataUtil.getStringValue(mo.get('detailId'));
                if(pdd.Id != null )details.add(pdd.Id);
                if(pdd.Id == null )pdd.PriceAdjustment__c = pad.Id;

                pdd.Product__c = DataUtil.getStringValue(mo.get('productId'));
                String psId = DataUtil.getStringValue(mo.get('psId'));

                if(String.isBlank(psId) )throw new DmlException('产品结构必填!');
                pL.add(new Product__c(Id = pdd.Product__c,ProductStructure__c=psId));
                setDetailValue(pdd,mo);
                pids.add(pdd.Product__c);

                paList.add(pdd);
                
                if(pdd.AdjustmentType__c == '封存')seals.add(pdd.Product__c);
            }

            //3、记录类型为子公司价格体系调整保存时，若发现产品有集团价且品牌为maccura且产品上【集团价封存】为false，但子公司要选择封存时，报错：集团价未封存不允许子公司封存***产品的价格体系！
            List<String> errors = new List<String>();
            for(Product__c p : [select Name  from Product__c where Brand__c ='maccura' and Id in:seals and GroupPriceSealed__c = false ] ){
                errors.add(p.Name);
            }
            if(!errors.isEmpty())throw new DmlException('集团价未封存不允许子公司封存' +String.join(errors,',')+'的价格体系');
            // List<String> errors = new List<String>();

            // for(Product__c ps: [select Id,NCCode__c,ProductStructure__c  from Product__c where Id  in : pids and ProductStructure__c = null  ]){
            //     errors.add(ps.NCCode__c);
            // }
            // if(!errors.isEmpty())msg = '存货编码为:'+String.join(errors,',')+'的产品无产品结构，请维护产品结构后申请价格体系！';
            // if( msg.length() > 0 ) throw new DmlException (msg);

           SavePoint sp =  Database.setSavepoint();
           try {

                if(!pL.isEmpty())update pL;

                delete [select Id from PriceAdjustmentDetail__c where  PriceAdjustment__c =:pad.Id and Id not in:details];
                if(!paList.isEmpty()) upsert paList;

               msg = '保存成功!';
               isSuc = true;

           }
           catch (Exception e) {
               Database.rollback(sp);
               throw e ;
           }
           


        }catch(Exception e ){
            isSuc = false;
            msg = '保存失败:'+e.getMessage();
        }
        

    }
    //赋值
    private void setDetailValue(PriceAdjustmentDetail__c pdd  , Map<String,Object> mo ){
        pdd.AdjustmentType__c = DataUtil.getStringValue(mo.get('adtype'));
        if(pad.RecordType.DeveloperName == PriceAdjustController.SUB_RECORDTYPE || pad.RecordType.DeveloperName == PriceAdjustController.ALL_RECORDTYPE){

            pdd.MarketPrice__c = DataUtil.getDecimalValue(mo.get('old_mk_price'));
            pdd.NewMarketPrice__c =  DataUtil.getDecimalValue(mo.get('new_mk_price'));

            pdd.AreaDealerPrice__c =  DataUtil.getDecimalValue(mo.get('old_d1_price'));
            pdd.NewAreaDealerPrice__c =  DataUtil.getDecimalValue(mo.get('new_d1_price'));

            pdd.InvoiceDealerPrice__c =  DataUtil.getDecimalValue(mo.get('old_d2_price'));
            pdd.NewInvoiceDealerPrice__c =  DataUtil.getDecimalValue(mo.get('new_d2_price'));

            pdd.HospitalManagerPrice__c =  DataUtil.getDecimalValue(mo.get('old_t_price'));
            pdd.NewHospitalManagerPrice__c  =  DataUtil.getDecimalValue(mo.get('new_t_price'));


            pdd.SpecialPrice__c =  DataUtil.getDecimalValue(mo.get('old_ds_price'));
            pdd.NewSpecialPrice__c  =  DataUtil.getDecimalValue(mo.get('new_ds_price'));

           


        }

         if(pad.RecordType.DeveloperName == PriceAdjustController.GROUP_RECORDTYPE){

            pdd.MarketPrice__c = DataUtil.getDecimalValue(mo.get('old_gmk_price'));
            pdd.NewMarketPrice__c =  DataUtil.getDecimalValue(mo.get('new_gmk_price'));

            pdd.AreaDealerPrice__c =  DataUtil.getDecimalValue(mo.get('old_gd1_price'));
            pdd.NewAreaDealerPrice__c =  DataUtil.getDecimalValue(mo.get('new_gd1_price'));

            pdd.InvoiceDealerPrice__c =  DataUtil.getDecimalValue(mo.get('old_gd2_price'));
            pdd.NewInvoiceDealerPrice__c =  DataUtil.getDecimalValue(mo.get('new_gd2_price'));

            pdd.HospitalManagerPrice__c =  DataUtil.getDecimalValue(mo.get('old_gt_price'));
            pdd.NewHospitalManagerPrice__c  =  DataUtil.getDecimalValue(mo.get('new_gt_price'));

            pdd.SpecialPrice__c =  DataUtil.getDecimalValue(mo.get('old_gds_price'));
            pdd.NewSpecialPrice__c  =  DataUtil.getDecimalValue(mo.get('new_gds_price'));

        }

        if(pad.RecordType.DeveloperName == PriceAdjustController.OVERSEA_RECORDTYPE){

            pdd.MarketPriceUSD__c = DataUtil.getDecimalValue(mo.get('old_b_price'));
            pdd.NewMarketPriceUSD__c =  DataUtil.getDecimalValue(mo.get('new_b_price'));

            pdd.HospitalBottomPrice__c =  DataUtil.getDecimalValue(mo.get('old_xs_price'));
            pdd.NewHospitalBottomPrice__c =  DataUtil.getDecimalValue(mo.get('new_xs_price'));

            pdd.SalesManagerPrice__c =  DataUtil.getDecimalValue(mo.get('old_jl_price'));
            pdd.NewSalesManagerPrice__c =  DataUtil.getDecimalValue(mo.get('new_jl_price'));

            pdd.SalesDirectorPrice__c =  DataUtil.getDecimalValue(mo.get('old_zj_price'));
            pdd.NewSalesDirectorPrice__c  =  DataUtil.getDecimalValue(mo.get('new_zj_price'));
        }



    }


    public class ProductBean{

        public String detailId ;
        //产品信息
        public String productName;
        public String productId;
        public String nccode;
        public String cate1;
        public String spec;
        public String unit;
        public String model;
        public String serialno;
        public String brand;
        public String adtype;

        //产品结构
        public String psName;
        public String psId;

        public Decimal instcost;
        //子公司
        //市场报价
        public Decimal old_mk_price;
        public Decimal new_mk_price;
        //一级经销商价
        public Decimal old_d1_price;
        public Decimal new_d1_price;
        //二级经销商价
        public Decimal old_d2_price;
        public Decimal new_d2_price;
        //终端价
        public Decimal old_t_price;
        public Decimal new_t_price;
         //特级经销商价(血糖)
        public Decimal old_ds_price;
        public Decimal new_ds_price;
       
        //省级总代(病理)
        public Decimal old_p_price;
        public Decimal new_p_price;


        //海外
        //标准报价
        public Decimal old_b_price;
        public Decimal new_b_price;
        //销售员价
        public Decimal old_xs_price;
        public Decimal new_xs_price;
        //总监价
        public Decimal old_zj_price;
        public Decimal new_zj_price;
        //经理价
        public Decimal old_jl_price;
        public Decimal new_jl_price;
        //集团

        //一级经销商价
        public Decimal old_gd1_price;
        public Decimal new_gd1_price;
        //二级经销商价
        public Decimal old_gd2_price;
        public Decimal new_gd2_price;
        //终端价
        public Decimal old_gt_price;
        public Decimal new_gt_price;
         //特级经销商价(血糖)
        public Decimal old_gds_price;
        public Decimal new_gds_price;
         //市场报价
        public Decimal old_gmk_price;
        public Decimal new_gmk_price;
        //省级总代(病理)
        public Decimal old_gp_price;
        public Decimal new_gp_price;

        public ProductBean(PriceAdjustmentDetail__c p ,Price__c pr, String type ){
            detailId = p.Id;
            productName = p.Product__r.ProductName__c;
            productId = p.Product__c;
            brand = p.Product__r.Brand__c;
            spec = p.Product__r.Spec__c;
            serialno = p.Product__r.SerialNo__c;
            model = p.Product__r.Model__c;
            unit = p.Product__r.Unit__c;
            cate1 =p.Product__r.Category1__c;
            nccode = p.Product__r.NCCode__c;
            adtype = p.AdjustmentType__c;
            psId = p.Product__r.ProductStructure__c;
            psName = p.Product__r.ProductStructure__r.Name ;
            if(pr != null ) instcost = pr.InstCost__c;

            if(type == PriceAdjustController.SUB_RECORDTYPE || type == PriceAdjustController.ALL_RECORDTYPE){

                old_mk_price = p.MarketPrice__c;
                new_mk_price = p.NewMarketPrice__c;

                old_d1_price = p.AreaDealerPrice__c;
                new_d1_price = p.NewAreaDealerPrice__c;

                old_d2_price = p.InvoiceDealerPrice__c;
                new_d2_price = p.NewInvoiceDealerPrice__c;

                old_t_price = p.HospitalManagerPrice__c;
                new_t_price = p.NewHospitalManagerPrice__c;


                // old_p_price = p.ProvincialAgencyPrice__c;
                // new_p_price = p.NewProvincialAgencyPrice__c;

                old_ds_price = p.SpecialPrice__c;
                new_ds_price = p.NewSpecialPrice__c;

                new_gmk_price = p.Product__r.GroupMarketPrice__c;
                new_gd1_price = p.Product__r.GroupRegionalDistributorsPrice__c;
                new_gd2_price = p.Product__r.GroupDeclarationDealerPrice__c;
                new_gt_price = p.Product__r.GroupTerminalGuidePric__c;

                new_gds_price  = p.Product__r.GroupSpecialPrice__c;
               // new_gp_price = p.Product__r.GroupProvincialAgencyPrice__c;


            }
            if(type == PriceAdjustController.GROUP_RECORDTYPE){

                old_gmk_price = p.MarketPrice__c;
                new_gmk_price = p.NewMarketPrice__c;

                old_gd1_price = p.AreaDealerPrice__c;
                new_gd1_price = p.NewAreaDealerPrice__c;

                old_gd2_price = p.InvoiceDealerPrice__c;
                new_gd2_price = p.NewInvoiceDealerPrice__c;

                old_gt_price = p.HospitalManagerPrice__c;
                new_gt_price = p.NewHospitalManagerPrice__c;


                old_gds_price = p.SpecialPrice__c;
                new_gds_price = p.NewSpecialPrice__c;

           


            }
            if(type == PriceAdjustController.OVERSEA_RECORDTYPE){

                old_b_price = p.MarketPriceUSD__c;
                new_b_price = p.NewMarketPriceUSD__c;

                old_xs_price = p.HospitalBottomPrice__c;
                new_xs_price = p.NewHospitalBottomPrice__c;

                old_zj_price = p.SalesDirectorPrice__c;
                new_zj_price = p.NewSalesDirectorPrice__c;

                old_jl_price = p.SalesManagerPrice__c;
                new_jl_price = p.NewSalesManagerPrice__c;

            }


        }


         public ProductBean(Product__c  p ,Price__c pr , String type ){
            
            productName = p.ProductName__c;
            productId = p.Id;
            brand = p.Brand__c;
            spec = p.Spec__c;
            serialno = p.SerialNo__c;
            model = p.Model__c;
            unit = p.Unit__c;
            cate1 =p.Category1__c;
            nccode = p.NCCode__c;

            new_gmk_price = p.GroupMarketPrice__c;
            new_gd1_price = p.GroupRegionalDistributorsPrice__c;
            new_gd2_price = p.GroupDeclarationDealerPrice__c;
            new_gt_price = p.GroupTerminalGuidePric__c;

            new_gds_price  = p.GroupSpecialPrice__c;
            psId = p.ProductStructure__c;
            psName = p.ProductStructure__r.Name ;

            if(pr != null){

                instcost = pr.InstCost__c;
                adtype = '更新';

                if(type == PriceAdjustController.SUB_RECORDTYPE || type == PriceAdjustController.ALL_RECORDTYPE ){

                    old_mk_price = pr.MarketPrice__c;
                    new_mk_price =  pr.MarketPrice__c;

                    old_d1_price = pr.AreaDealerPrice__c;
                    new_d1_price = pr.AreaDealerPrice__c;

                    old_d2_price = pr.InvoiceDealerPrice__c;
                    new_d2_price = pr.InvoiceDealerPrice__c;

                    old_t_price = pr.HospitalManagerPrice__c;
                    new_t_price = pr.HospitalManagerPrice__c;


                    old_ds_price = pr.SpecialPrice__c;
                    new_ds_price = pr.SpecialPrice__c;


                    


                }
               
               

                if(type == PriceAdjustController.OVERSEA_RECORDTYPE){

                    old_b_price = pr.MarketPriceUSD__c;

                    old_xs_price = pr.SalesmanPrice__c;

                    old_zj_price = pr.SalesDirectorPrice__c;

                    old_jl_price = pr.SalesManagerPrice__c;


                    new_b_price = pr.MarketPriceUSD__c;

                    new_xs_price = pr.SalesmanPrice__c;

                    new_zj_price = pr.SalesDirectorPrice__c;

                    new_jl_price = pr.SalesManagerPrice__c;

                }

            }else{

                adtype ='新增'; 

            }



            if(type == PriceAdjustController.GROUP_RECORDTYPE){

                    old_gmk_price = p.GroupMarketPrice__c;

                    old_gd1_price = p.GroupRegionalDistributorsPrice__c;

                    old_gd2_price = p.GroupDeclarationDealerPrice__c;

                    old_gt_price = p.GroupTerminalGuidePric__c;


                    old_gds_price = p.GroupSpecialPrice__c;



                    new_gmk_price = p.GroupMarketPrice__c;

                    new_gd1_price = p.GroupRegionalDistributorsPrice__c;

                    new_gd2_price = p.GroupDeclarationDealerPrice__c;

                    new_gt_price = p.GroupTerminalGuidePric__c;


                    new_gds_price = p.GroupSpecialPrice__c;


                    adtype = old_gmk_price == null && 
                             old_gd1_price == null && 
                             old_gd2_price == null && 
                             old_gt_price == null && 
                             old_gds_price == null && 
                             old_gp_price == null ?  '新增' : '更新';

            }

        }

    }


}